package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.mapper.OrderMapper;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.date.DateUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper,Order> implements OrderService {

    @Autowired
    private OrderSettingService orderSettingService;

    @Autowired
    private MemberService memberService;

    @Override
    public Order add(Map map) {
        Order order = new Order();
        // 判断是否可以预约
        // 预约日期
        String orderDate = (String) map.get("orderDate");
        // 电话
        String telephone = (String) map.get("telephone");
        // 套餐号
        String setmealId = (String) map.get("setmealId");

        order.setOrderDate(DateUtils.parseString2Date(orderDate,"yyyy-MM-dd"));
        order.setOrderType((String) map.get("orderType"));
        order.setSetmealId(Integer.parseInt(setmealId));
        order.setOrderStatus("未到诊");

        int count = orderSettingService.isOrderSettingOK(orderDate);
        if (count > 0) {
            // 可以预约
            Member member = memberService.findMemberByTelephone(telephone);
            // 判断会员新还是老
            if (member != null) {
                // 老会员 判断是否重复预约 根据会员号 套餐号 日期查询订单表
                int isExist = baseMapper.queryOrderIsExist(member.getId(),orderDate,Integer.parseInt(setmealId));
                if (isExist > 0) {
                    // 说明已经预约过了
                    throw new RuntimeException("请勿重复预约！");
                } else {
                    // 没有预约过，可以预约 t_order增加一条订单 insert t_ordersetting表 已预约数+1 update
                    order.setMemberId(member.getId());
//                    t_order增加一条订单
                    baseMapper.insert(order);
                        //t_ordersetting表 已预约数+1 update
                    orderSettingService.updateResetvationSByOrderDate(orderDate);
                }
            } else {
                // 新会员，可以预约 t_member增加操作
                String name = (String) map.get("name");
                String sex = (String) map.get("sex");
                String idCard = (String) map.get("idCard");
                member = new Member();
                member.setName(name);
                member.setSex(sex);
                member.setIdCard(idCard);
                member.setPhoneNumber(telephone);
                member.setRegTime(new Date(System.currentTimeMillis()));
                memberService.save(member);
                order.setMemberId(member.getId());
                baseMapper.insert(order);
                //t_ordersetting表 已预约数+1 update
                orderSettingService.updateResetvationSByOrderDate(orderDate);
            }
        } else {
            // 不可以预约
            throw new RuntimeException("已经预约满了，请选择其他日期");
        }
        return order;
    }

    @Override
    public Map findOrderInfoByorderId(Integer id) {
        return baseMapper.findOrderInfoByorderId(id);
    }
}
